package com.auto.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auto.entity.Site;
import com.auto.rep.SiteRep;
import com.google.common.collect.Lists;

@Controller
public class SiteController {

	@Autowired
	private SiteRep siteRep;

	@RequestMapping(value = "site", method = RequestMethod.GET)
	@ResponseBody
	public List<Site> getSites() {
		return Lists.newArrayList(siteRep.findAll());
	}

	@RequestMapping(value = "site/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Site getSite(@PathVariable("id") Long id) {
		return siteRep.findOne(id);
	}

	@RequestMapping(value = "site/{id}", method = RequestMethod.POST)
	@ResponseBody
	public void updateSite(@PathVariable("id") Long id, Site site) {
		site.setId(id);
		siteRep.save(site);
	}

	@RequestMapping(value = "site/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteSite(@PathVariable("id") Long id) {
		siteRep.delete(id);
	}

	@RequestMapping(value = "site", method = RequestMethod.POST)
	@ResponseBody
	public Site addSite(Site site) {
		return siteRep.save(site);
	}
}
